// import http from '/util/request.js';
let app = getApp();
Page({
  data: {
    username: '',
    password: '',
    isShow: false,  //显示弹窗
    describe: "",
    buttonList: [{
      name: '确认',
    }],

    isLoginInformation: false,
    page: "",//返回跳转进入的页面路径
    stop: 0,    //自动登录

  },
  onLoad(e) {
    var that = this;
    if (e.page) {
      that.setData({
        page: e.page
      })
    }
    if (e.stop) {
      that.setData({
        stop: e.stop
      })
    }
    dd.getStorage({
      key: 'loginMember',
      success: function (res) {
        var isLoginInformation = res.data.isLoginInformation;
        if (isLoginInformation) {
          that.setData({
            isLoginInformation: true
          })
          var res = dd.getStorageSync({ key: 'loginMember' });
          console.log("res==>", res);
          that.setData({
            username: res.data.username,
            password: res.data.password,
            isLoginInformation: res.data.isLoginInformation
          })
          if (that.data.stop == 0) {
            that.onPrimLargeTap();
          }
          // that.getRemember();
        } else {
          that.setData({
            isLoginInformation: false
          })
        }

      },
      fail: function (res) {
        that.setData({
          isLoginInformation: false
        })
      }
    });

    that.setLoginAuthentication();
    // that.onLoginShuju();

  },

  // 监听用户输入的内容
  onTapUser(e) {
    console.log("1", e.detail.value);
    var that = this;
    that.setData({
      username: e.detail.value
    })
  },
  // 监听密码输入的内容
  onTapPassword(e) {
    console.log("2", e.detail.value);
    var that = this;
    that.setData({
      password: e.detail.value
    })
  },
  // 清除输入的用户号
  onClearUser(e) {
    var that = this;
    that.setData({
      username: ''
    })
  },

  // 清除输入的用户名
  onClearPassword(e) {
    var that = this;
    that.setData({
      password: ''
    })

  },

  setLoginAuthentication(){
    var that = this;  

    var data = {
      "key": "66249c7f42e1e400",
      "plain_text": "东阳利世,sa,(+s6q^W_rr@L"
    }

    dd.httpRequest({
      url: 'http://cloud.reece-sewing.com:9351/sql_login',
      method: 'POST',
      headers: {
        "Content-Type": "application/json"
      },
      data: JSON.stringify(data),
      dataType: 'json',
      success: function (res) {
        console.log("1111111111111",res)
        if (res.status == 200) {
        console.log("11111111111111200",res)
        app.globaldata.cipher_code = res.data;
        console.log("111111111111111111111",app.globaldata.cipher_code);
        
        } else {
          dd.showToast({
            type: 'fail',
            content: res.data.data,
            duration: 3000,
          });
        }
      },
      fail: function (res) {
        dd.showToast({
          type: 'fail',
          content: res.errorMessage,
          duration: 2000,
        });
      },
      complete: function (res) {
        // dd.alert({ content: 'complete' });
        // my.hideLoading();
      }
    });
  },

  // onLoginShuju(){
  //   console.log("触发了logon2");
  //   var that = this;  
  //   var username = that.data.username;
  //   var password = that.data.password;

  //   console.log("111111111111111222222222",username);
  //   console.log("11111111222222222",password);

  //   var data = {
  //     "staff": {
  //       "key": "65af0c304f878e00",
  //       "encrypted_text": app.globaldata.cipher_code,
  //       "pwd_field": "F_ZYKJ_Text7"
  //     },
  //     "user": {
  //       "username": username,
  //       "password": password
  //     }
  //   }

  //   console.log("555555555555",app.globaldata.cipher_code);
  //   console.log("555555555555",username);
  //   console.log("555555555555",password);

  //   dd.httpRequest({
  //     url: 'http://cloud.reece-sewing.com:9351/king_staff_query',
  //     method: 'POST',
  //     headers: {
  //       "Content-Type": "application/json"
  //     },
  //     data: JSON.stringify(data),
  //     dataType: 'json',
  //     success: function (res) {
  //       console.log("222222222222",res)
  //       if (res.data.code == 200) {
  //       console.log("2222222222222200",res)
        
  //       } else {
  //         dd.showToast({
  //           type: 'fail',
  //           content: res.data.data,
  //           duration: 3000,
  //         });
  //       }
  //     },
  //     fail: function (res) {
  //       dd.showToast({
  //         type: 'fail',
  //         content: res.errorMessage,
  //         duration: 2000,
  //       });
  //     },
  //     complete: function (res) {
  //       // dd.alert({ content: 'complete' });
  //       // my.hideLoading();
  //     }
  //   });
  // },

  // 点击登陆的绑定事件 以及发起网络请求
  onPrimLargeTap() {
    var that = this;  
    var username = that.data.username;
    var password = that.data.password;
    var isLoginInformation = that.data.isLoginInformation;
    console.log("66666666666",username);
    console.log("666666666665",password);

    var data = {
      "xx": {
        "formId": "BD_Empinfo",
        "fields": [
          "FID","FName","FStaffNumber","F_RICH_PWD","FPost.fname","FPostDept.fname"
        ],
        "FilterString": "FStaffNumber='"+ username +"'and F_RICH_PWD='"+ password +"'",
        "orderby": "",
        "topRowCount": 0,
        "limit": 0,
        "StartRow": 0,
        "fields_name": [
          "实体主键","员工姓名","员工编号","用户密码","就任岗位","所属部门"
        ],
        "acctID": "66249c7f42e1e4"
      },
      "login": {
        "username": "test",
        "password": "zz333333"
      }
    }
    // dd.reLaunch({
    //   url: '/pages/index/index',
    // }) 
    // return

    dd.httpRequest({
      url: 'http://cloud.reece-sewing.com:9351/king_query',
      method: 'POST',
      headers: {
        "Content-Type": "application/json"
      },
      data: JSON.stringify(data),
      dataType: 'json',
      success: function (res) {
        // dd.alert({ content: 'success' + res.data.id });
        console.log("小数据查询员工信息响应==>",res)
        if (res.data.code == 200) {

        console.log("小数据查询员工信息响应200==>",res)

          console.log("22222222")
          app.globaldata.user.FID = res.data.data[0]['实体主键'];
          // app.globaldata.user.token = res.data.data[0]['token'];
          // app.globaldata.user.上级部门id = res.data.data[0]['上级部门id'];
          // app.globaldata.user.上级部门名称 = res.data.data[0]['上级部门名称'];
          app.globaldata.user.员工姓名 = res.data.data[0]['员工姓名'];
          app.globaldata.user.员工工号 = res.data.data[0]['员工编号'];
          // app.globaldata.user.审核状态 = res.data.data[0]['审核状态'];
          // app.globaldata.user.岗位ID = res.data.data[0]['岗位ID'];
          app.globaldata.user.岗位名称 = res.data.data[0]['就任岗位'];
          // app.globaldata.user.岗位编码 = res.data.data[0]['岗位编码'];
          // app.globaldata.user.性别ID = res.data.data[0]['性别ID'];
          app.globaldata.user.用户密码 = res.data.data[0]['用户密码'];
          // app.globaldata.user.电子邮箱 = res.data.data[0]['电子邮箱'];
          // app.globaldata.user.禁用状态 = res.data.data[0]['禁用状态'];
          // app.globaldata.user.联系电话 = res.data.data[0]['联系电话'];
          // app.globaldata.user.部门id = res.data.data[0]['部门id'];
          // app.globaldata.user.部门全称 = res.data.data[0]['部门全称'];
          app.globaldata.user.部门名称 = res.data.data[0]['所属部门'];

          console.log('app.globaldata.user==>',app.globaldata.user);
          dd.reLaunch({
            url: '/pages/index/index',
          }) 
          var loginMember = {
            username: username,
            password: password,
            isLoginInformation: isLoginInformation
          }
          that.setRemember(loginMember)
        } else {
          // dd.alert({ content: res.data.data ,buttonText: '我知道了'});
          dd.showToast({
            type: 'fail',
            content: res.data.data,
            duration: 3000,
          });
        }
      },
      fail: function (res) {
        // dd.alert({ content: 'fail' });
        dd.showToast({
          type: 'fail',
          content: res.errorMessage,
          duration: 2000,
        });
        // dd.alert({ content: res.errorMessage });
      },
      complete: function (res) {
        // dd.alert({ content: 'complete' });
        // my.hideLoading();
      }
    });

  },

  //关闭弹窗
  closePopover(e) {
    this.setData({
      isShow: false
    })
  },

  //记录登录信息 
  onSwitchChange(e) {
    console.log("e==>", e);
    var that = this;
    that.setData({
      isLoginInformation: e.detail.value
    })
    if (!e.detail.value) {
      dd.removeStorage({
        key: 'loginMember',
        success: function () {
          console.log("清除账号密码缓存")
        }
      });
    }
  },

  //获取缓存密码   
  getRemember() {
    var that = this;
    var res = dd.getStorageSync({ key: 'loginMember' });
    console.log("res==>", res);
    that.setData({
      username: res.data.username,
      password: res.data.password,
      isLoginInformation: res.data.isLoginInformation
    })


  },
  //存储登录密码
  setRemember(loginMember) {
    var data = loginMember;
    dd.setStorage({
      key: 'loginMember',
      data: data,
      success: function () {
        // dd.alert({content: '写入成功'});
      }
    });
  },

  setFenLeilist() {
    // app.https('加载中..','GET', "http://intlrich.com:8084/leibie",{}).then(res => {
    //     console.log("获取分类信息=>",res.data);
    //     app.globaldata.Sortlist = res.data
    //   })
  },




  uplodeMethod() {
    dd.showLoading({
      content: '正在获取新版本中...',
    });

    var updateManager = dd.getUpdateManager();
    console.log(updateManager);
    updateManager.onCheckForUpdate(function (res) {
      console.log("res==>", res);
      if (res.hasUpdate) {
        updateManager.onUpdateReady(function (ret) {
          console.log(ret.version) // 更新版本号
          dd.hideLoading()
          dd.confirm({
            title: '更新提示',
            content: '新版本已经准备好，是否重启应用？',
            success: function (res) {
              if (res.confirm) {
                updateManager.applyUpdate()
              }
            }
          })
        })
      } else {
        dd.hideLoading()
        dd.alert({
          title: '已经是新版本了！',
          buttonText: '我知道了',
          success: () => { },
        })
      }
      dd.hideLoading()
    })
    updateManager.onUpdateFailed(function () {
      dd.hideLoading()
      // 新版本下载失败
      dd.alert({
        title: '新版本下载失败！请稍后重试！',
        buttonText: '我知道了',
        success: () => { },
      })
    })

  },



});
